package com.superbytecode.cloud.rocketmq.producer.provider;

import com.aliyun.openservices.ons.api.Message;
import com.aliyun.openservices.ons.api.Producer;
import com.aliyun.openservices.ons.api.SendResult;
import com.aliyun.openservices.ons.api.order.OrderProducer;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.datatype.jsr310.JavaTimeModule;
import com.superbytecode.cloud.common.model.mq.MQConfigure;
import com.superbytecode.cloud.rocketmq.exception.MQBizException;
import com.superbytecode.cloud.rocketmq.producer.MQProduceProvider;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;

import javax.annotation.Resource;
import java.util.Objects;
import java.util.Optional;

/**
 * All rights Reserved, Designed By www.super-master.com
 *
 * @ProjectName: beacon-cloud
 * @Package: com.superbytecode.cloud.rocketmq.producer
 * @ClassName: AliRocketMQProducer
 * @Description: [阿里云RocketMq生产信息实现]
 * <p> </p>
 * @Author: [Lance Ting]
 * @Date: 2024/11/1 16:31
 * @Version: V1.0
 * @Copyright: 2024 www.super-master.com Inc. All rights reserved.
 * TODO: 注意,本文件Lance Ting所作,如果转载或使用请标明具体出处!
 **/
@Slf4j
public class AliMQProduceProvider implements MQProduceProvider {

    @Resource
    private Producer producer;

    @Resource
    private OrderProducer orderProducer;

    private final ObjectMapper objectMapper = new ObjectMapper();

    public AliMQProduceProvider(JavaTimeModule javaTimeModule) {
        objectMapper.registerModule(javaTimeModule);
    }

    /**
     * mq生产者
     *
     * @param topic     消息主题
     * @param configure 消息参数配置
     * @param body      消息内容
     */
    @Override
    public void producer(String topic, MQConfigure configure, Object body) {
        // com.aliyun.openservices.ons.api.Message;
        Message aliMQMessage = this.obtainAliMQMessage(topic, configure, body);
        // 执行消息发送
        SendResult sendResult;
        String hash = configure.getHash();
        if (StringUtils.isNotBlank(hash)) {
            sendResult = orderProducer.send(aliMQMessage, hash);
        } else {
            sendResult = producer.send(aliMQMessage);
        }
        log.info("aliMQ send result topic [{}] msgId [{}] ", topic, sendResult.getMessageId());
    }

    /**
     * 获取阿里MQ消息对象
     *
     * @param topic     消息主题
     * @param configure 配置
     * @param body      消息内容
     * @return com.aliyun.openservices.ons.api.Message
     */
    private Message obtainAliMQMessage(String topic, MQConfigure configure, Object body) {
        Message aliMQMessage = new Message();
        aliMQMessage.setTopic(topic);
        try {
            byte[] bodyBytes = objectMapper.writeValueAsBytes(body);
            aliMQMessage.setBody(bodyBytes);
        } catch (JsonProcessingException e) {
            throw new MQBizException(e);
        }
        // 构建消息参数
        configure = Optional.ofNullable(configure).orElse(new MQConfigure());
        String tag = configure.getTag();
        if (StringUtils.isNotBlank(tag)) {
            aliMQMessage.setTag(tag);
        }
        String hash = configure.getHash();
        if (StringUtils.isNotBlank(hash)) {
            aliMQMessage.setShardingKey(hash);
            aliMQMessage.setKey(hash);
        }
        Long deliverTime = configure.getDeliverTime();
        if (Objects.nonNull(deliverTime)) {
            aliMQMessage.setStartDeliverTime(deliverTime);
        }
        return aliMQMessage;
    }
}